package ue.client.planes;

import java.util.ArrayList;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;

import ue.client.core.Game;
import ue.client.core.Vector2d;
import ue.client.galaxy.Location;

public class Subspace {
	public static Vector2d viewPos;

	public static final int DIST = 100;
	public static final float[] color = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };

	public static ArrayList<Location> locations;

	public static void init() {
		locations = new ArrayList<Location>();
		
		viewPos = new Vector2d();
	}

	public static void tick() {
        int xTmp = (int)(viewPos.x - Game.WIDTH / 2);
        int yTmp = (int)(viewPos.y - Game.HEIGHT / 2);

		for(int y = -1; y < Game.HEIGHT / DIST + 1; y++) {
			for(int x = -1; x < Game.WIDTH / DIST + 1; x++) {
				int i = findLocation(xTmp / DIST + x, yTmp / DIST + y);

				if (i < 0) {
					getLocationData(xTmp / DIST + x, yTmp / DIST + y);
				}
			}
		}
		
		viewPos.x += 1;
	}

	public static void render() {
//		
//		
//		for(int y = (int)(viewPos.y % DIST); y < (int)(viewPos.y % DIST); y += DIST) {
//			Game.drawLine(0, y, Game.WIDTH, y, color);
//		}
//
//		for(int x = (int)(viewPos.x % DIST); x < (int)(viewPos.x % DIST); x += DIST) {
//			Game.drawLine(x, 0, x, Game.HEIGHT, color);
//		}
        
		for(int y = (int)(viewPos.y - Game.HEIGHT / 2  - DIST); y < (int)(viewPos.y + Game.HEIGHT / 2 + DIST); y += DIST) {
			for(int x = (int)(viewPos.x - Game.WIDTH / 2 - DIST); x < (int)(viewPos.x + Game.WIDTH / 2 + DIST); x += DIST) {
				
				int xTmp = (int)(x / DIST);
				int yTmp = (int)(y / DIST);
				
				Game.drawEmptyRectangle((float)(xTmp * DIST - viewPos.x + Game.WIDTH / 2), (float)(yTmp * DIST - viewPos.y + Game.HEIGHT / 2), 
						(float)(xTmp * DIST - viewPos.x + Game.WIDTH / 2 + DIST), (float)(yTmp * DIST - viewPos.y + Game.HEIGHT / 2 * DIST), color);
				
				int i = findLocation(xTmp, yTmp);
				if (i >= 0) {
					GL11.glEnable(GL11.GL_TEXTURE_2D);
					
					Game.font.drawString((float)(xTmp * DIST - viewPos.x + Game.WIDTH / 2) + 2, (float)(yTmp * DIST - viewPos.y + Game.HEIGHT / 2) + 2, "X: " + xTmp + " Y: " + yTmp);
					Game.font.drawString((float)(xTmp * DIST - viewPos.x + Game.WIDTH / 2) + 2, (float)(yTmp * DIST - viewPos.y + Game.HEIGHT / 2) + 19, locations.get(i).getName(), Color.white);
					Game.font.drawString((float)(xTmp * DIST - viewPos.x + Game.WIDTH / 2) + 2, (float)(yTmp * DIST - viewPos.y + Game.HEIGHT / 2) + 36, locations.get(i).getAsteroidAmount() + " Asteroids", Color.white);

					GL11.glDisable(GL11.GL_TEXTURE_2D);
				}
//				System.out.println("X" + x + ";Y" + y);
			}
		}
	}

	public static int findLocation(int x, int y) {
		for (int i = 0; i < locations.size(); i++) {
			if (locations.get(i).getX() == x && locations.get(i).getY() == y) {
				return i;
			}
		}

		return -1;
	}
	
	public static void getLocationData(int x, int y) {
		if(findLocation(x, y) < 0) {
			locations.add(new Location(x,y));
		}
	}

	public static void requestLocation(int x, int y) {
		Game.client.requestLocation(x, y);
	}
}
